package com.dabai.dbutils.utils.component;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;

import java.util.List;

/**
 * activity工具类
 * @author 故事与猫
 */
public class ActivityUtils {


    /**
     * 开始活动的结果
     *
     * @param activity 活动
     * @param classZ   classz
     * @param reqCode  申请代码
     */
    public static void startActivityForResult(Activity activity,Class classZ,int reqCode){
        // 跳转逻辑
        Intent intent = new Intent(activity, classZ);
        activity.startActivityForResult(intent, reqCode);
    }


    /**
     * 是第一个打开的活动
     *
     * @param activity 活动
     * @return boolean
     */
    public static boolean isFirstActivity(Activity activity){
        PackageManager packageManager = activity.getApplication().getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(activity.getPackageName());
        ComponentName launchComponentName = intent.getComponent();
        ComponentName componentName = activity.getComponentName();
        return componentName.toString().equals(launchComponentName.toString());
    }


    /**
     * 判断是否运行在前台
     *
     * @param context 上下文
     * @return boolean
     */
    public static boolean isRunningForeground(Context context) {
        String packageName = context.getPackageName();
        String topActivityClassName = getTopActivityName(context);
        return packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName);
    }


    /**
     * 获取显示在最顶端的activity名称
     *
     * @param context 上下文
     * @return {@link String}
     */
    public static String getTopActivityName(Context context) {
        String topActivityClassName = null;
        ActivityManager manager = (ActivityManager) (context.getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> taskInfo = manager.getRunningTasks(1);
        if (taskInfo != null) {
            ComponentName f = taskInfo.get(0).topActivity;
            topActivityClassName = f.getClassName();
        }
        return topActivityClassName;
    }



    /**
     * 启动活动
     * @param activity 当前活动
     * @param classZ 目标类
     */
    public static void startActivity(Activity activity,Class classZ){
        activity.startActivity(new Intent(activity,classZ));
    }

    /**
     * 启动活动(带共享元素的)
     *
     * @param activity       当前活动
     * @param classZ         目标类
     * @param view           视图
     * @param transitionName 过渡的名字
     */
    public static void startActivity(Activity activity, Class classZ, View view,String transitionName){
        ActivityCompat.startActivity(activity, new Intent(activity,classZ),
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, transitionName).toBundle());
    }

    /**
     * 启动活动(带共享元素的)
     *
     * @param activity       当前活动
     * @param view           视图
     * @param transitionName 过渡的名字
     * @param intent         意图
     */
    public static void startActivity(Activity activity,Intent intent, View view,String transitionName){
        ActivityCompat.startActivity(activity, intent,
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, transitionName).toBundle());
    }

    /**
     * 启动活动
     * @param context 当前上下文
     * @param classZ 目标类
     */
    public static void startActivity(Context context,Class classZ){
        context.startActivity(new Intent(context,classZ));
    }

    /**
     * 重启活动
     * @param activity 当前活动
     */
    public static void restartActivity(Activity activity){
        activity.startActivity(new Intent(activity, activity.getClass()));
        activity.finish();
    }


}
